import {createRouter, createWebHashHistory} from 'vue-router'


/* Layout */
import Layout from '@/layout'
import Login from "@/views/Login.vue";

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {requiresAuth: false, hideLayout: true }
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('@/views/Register.vue'),
        meta: { requiresAuth: false }
    },
    {
        path: '/forgot-password',
        name: 'ForgotPassword',
        component: () => import('@/views/ForgotPassword.vue'),
        meta: { requiresAuth: false }
    },
    {
        path: '/',
        name: 'index',
        component: Layout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '', // 默认子路由
                name: 'index',
                component: () => import('@/views/AppMain.vue')
            }
        ]
    },
    {
        path: '/tools',
        name: 'tools',
        component: Layout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '', // 默认子路由
                name: 'tools',
                component: () => import('@/views/tools/index.vue')
            }
        ]
    },
    {
        path: '/setting',
        name: 'setting',
        component: Layout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '', // 默认子路由
                name: 'setting',
                component: () => import('@/views/setting/index.vue')
            }
        ]
    },
    {
        path: '/docs',
        name: 'docs',
        component: Layout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '', // 默认子路由
                name: 'docs',
                component: () => import('@/views/docs/index.vue')
            }
        ]
    },
    {
        path: '/tutorial',
        name: 'tutorial',
        component: Layout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '', // 默认子路由
                name: 'tutorial',
                component: () => import('@/views/tutorial/index.vue')
            }
        ]
    },
    {
        path: '/graph',
        name: 'graph',
        component: Layout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '', // 默认子路由
                name: 'graph',
                component: () => import('@/views/graph/index.vue')
            }
        ]
    },
    {
        path: '/management',
        name: 'management',
        component: Layout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '', // 默认子路由
                name: 'management',
                component: () => import('@/views/management/index.vue')
            }
        ]
    },
    {
        path: '/dataview',
        name: 'dataview',
        component: Layout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '', // 默认子路由
                name: 'dataview',
                component: () => import('@/views/dataview/index.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token') // 先用简单方式检查
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  const hideLayout = to.matched.some(record => record.meta.hideLayout)

  if (requiresAuth && !isAuthenticated) {
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
  } else if (to.path === '/login' && isAuthenticated) {
    next('/')
  } else {
    // 处理布局显示
    if (hideLayout) {
      document.querySelector('.common-layout')?.classList.add('hidden-layout')
    } else {
      document.querySelector('.common-layout')?.classList.remove('hidden-layout')
    }
    next()
  }
})
export default router
